package com.example.qianjun.gobang01.robot_player;

import android.graphics.Point;
import android.util.Log;

import com.example.qianjun.gobang01.bean.Constant;
import com.example.qianjun.gobang01.bean.GobangCheckboard;
import com.example.qianjun.gobang01.util.ReferenceInit;

import java.util.Arrays;

/**
 * 棋子的估值
 * Created by qianjun on 2016/5/15.
 */
public class EvaluePointScore {

    public static GobangCheckboard gobangCheckboard;
    public static int[][] tableCheckboard;
    public static int table_x;
    public static int table_y;

    //位置得分的棋盘
    public static int[][] tablePositionScore;

    //估值的时候，存储起始与结束棋子的坐标
    public static Point headPoint = new Point(Constant.NO_POINT.x, Constant.NO_POINT.y);
    public static Point endPoint = new Point(Constant.NO_POINT.x, Constant.NO_POINT.y);

    public static void evaluepointScoreInit() {
        //获取棋盘数据
        gobangCheckboard = ReferenceInit.getGobangCheckboard();
        tableCheckboard = gobangCheckboard.getTable();
        table_x = gobangCheckboard.getTable_x();
        table_y = gobangCheckboard.getTable_y();


        switch (table_x) {
            case Constant.SMALL_CHECKBOARD_X:
                tablePositionScore = Constant.SMALL_TABLE_POSITION_SCORE;
                break;
            case Constant.MIDDDLE_CHECKBOARD_X:
                tablePositionScore = Constant.MIDDLE_TABLE_POSITION_SCORE;
                break;
            case Constant.LARGE_CHECKBOARD_X:
                tablePositionScore = Constant.LARGE_TABLE_POSITION_SCORE;
                break;
        }


    }


    //获取水平方向的棋型得分
    public static int getHorizontal(int i, int j, int piecesTYPE, int[][] table) {



        //空白点位与当前类型棋子出现的次数
        int count = 1;
        //循环变量
        int m = i - 1;
        //棋盘的位置得分
        int positionScore = 0;
        //棋型得分
        int chessStyleScore = 0;

        for (m = i - 1; m >= 0; m--) {
            if ((table[m][j] == piecesTYPE) || table[m][j] == Constant.NO_PIECES) {
                count++;
            } else {
                break;
            }
        }

        headPoint = new Point(m, j);

        for (m = i + 1; m < table_x; m++) {
            if ((table[m][j] == piecesTYPE) || (table[m][j] == Constant.NO_PIECES)) {
                count++;
            } else {

                break;
            }
        }
        endPoint = new Point(m, j);


        char[] points = new char[count];
        //将棋盘数据转化为棋型数据
        for (m = headPoint.x + 1; m < endPoint.x; m++) {


            if (table[m][j] == piecesTYPE) {

                //获取棋盘位置得分
                positionScore += tablePositionScore[m][j];

                points[m - (headPoint.x + 1)] = '1';
            } else if (table[m][j] == Constant.NO_PIECES) {
                points[m - (headPoint.x + 1)] = '0';
            }
        }

        points[i - (headPoint.x + 1)] = '1';
        //当前要落子的点位的得分
        positionScore += tablePositionScore[i][j];

        //存储棋型的字符串
        String chessStyle = new String(points);
        for (m = 0; m < Constant.chessStyleFives.length; m++) {
            if (chessStyle.indexOf(Constant.chessStyleFives[m].getChessStyle()) >= 0) {
                chessStyleScore = Constant.chessStyleFives[m].getChessStyleScore();
                break;
            }
        }

        Log.i("mike","---------------------------------------");
        Log.i("mike","该点坐标：x="+i+" y="+j);
        Log.i("mike","水平方向得分："+positionScore + chessStyleScore);
        Log.i("mike","水平方向棋型："+chessStyle);
        //返回位置得分与棋型得分之和
        return positionScore + chessStyleScore;
    }

    //--------------------------------------------------------------------------------------------------
    //获取垂直方向的棋型得分
    public static int getVertical(int i, int j, int piecesTYPE, int[][] table) {

        //空白点位与当前类型棋子出现的次数
        int count = 1;
        //循环变量
        int m = j - 1;
        //棋盘的位置得分
        int positionScore = 0;
        //棋型得分
        int chessStyleScore = 0;

        for (m = j - 1; m >= 0; m--) {
            if ((table[i][m] == piecesTYPE) || table[i][m] == Constant.NO_PIECES) {
                count++;
            } else {
                break;
            }
        }

        headPoint = new Point(i, m);

        for (m = j + 1; m < table_y; m++) {
            if ((table[i][m] == piecesTYPE) || table[i][m] == Constant.NO_PIECES) {
                count++;
            } else {

                break;
            }
        }
        endPoint = new Point(i, m);


        char[] points = new char[count];
        //将棋盘数据转化为棋型数据
        for (m = headPoint.y + 1; m < endPoint.y; m++) {


            if (table[i][m] == piecesTYPE) {

                //获取棋盘位置得分
                positionScore += tablePositionScore[i][m];

                points[m - (headPoint.y + 1)] = '1';
            } else if (table[i][m] == Constant.NO_PIECES) {
                points[m - (headPoint.y + 1)] = '0';
            }
        }

        points[j - (headPoint.y + 1)] = '1';

        //当前要落子的点位的得分
        positionScore += tablePositionScore[i][j];


        //存储棋型的字符串
        String chessStyle = new String(points);
        for (m = 0; m < Constant.chessStyleFives.length; m++) {
            if (chessStyle.indexOf(Constant.chessStyleFives[m].getChessStyle()) >= 0) {
                chessStyleScore = Constant.chessStyleFives[m].getChessStyleScore();
                break;
            }
        }


        Log.i("mike","垂直方向得分："+positionScore + chessStyleScore);
        Log.i("mike","垂直方向棋型："+chessStyle);

        //返回位置得分与棋型得分之和
        return positionScore + chessStyleScore;
    }

    //--------------------------------------------------------------------------------------------------
    //计算该点棋型左上右下得分
    public static int getLeft(int i, int j, int piecesTYPE, int[][] table) {

        //空白点位与当前类型棋子出现的次数
        int count = 1;
        //循环变量
        int m = i - 1;
        int n = j - 1;
        //棋盘的位置得分
        int positionScore = 0;
        //棋型得分
        int chessStyleScore = 0;

        for (m = i - 1, n = j - 1; m >= 0 && n >= 0; m--, n--) {
            if ((table[m][n] == piecesTYPE) || table[m][n] == Constant.NO_PIECES) {
                count++;
            } else {
                break;
            }
        }

        headPoint = new Point(m, n);

        for (m = i + 1, n = j + 1; m < table_x && n < table_y; m++, n++) {
            if ((table[m][n] == piecesTYPE) || table[m][n] == Constant.NO_PIECES) {
                count++;
            } else {

                break;
            }
        }
        endPoint = new Point(m, n);


        char[] points = new char[count];
        //将棋盘数据转化为棋型数据
        for (m = headPoint.x + 1, n = headPoint.y + 1; m < endPoint.x && n < endPoint.y; m++, n++) {


            if (table[m][n] == piecesTYPE) {

                //获取棋盘位置得分
                positionScore += tablePositionScore[m][n];
                points[m - (headPoint.x + 1)] = '1';
            } else if (table[m][n] == Constant.NO_PIECES) {
                points[m - (headPoint.x + 1)] = '0';
            }
        }

        points[i - (headPoint.x + 1)] = '1';

        //当前要落子的点位的得分
        positionScore += tablePositionScore[i][j];

        //存储棋型的字符串
        String chessStyle = new String(points);
        for (m = 0; m < Constant.chessStyleFives.length; m++) {
            if (chessStyle.indexOf(Constant.chessStyleFives[m].getChessStyle()) >= 0) {
                chessStyleScore = Constant.chessStyleFives[m].getChessStyleScore();
                break;
            }
        }


        Log.i("mike","左上右下方向得分："+positionScore + chessStyleScore);
        Log.i("mike","左上右下方向棋型："+chessStyle);


        //返回位置得分与棋型得分之和
        return positionScore + chessStyleScore;

    }

    //--------------------------------------------------------------------------------------------------
    //计算该点棋型左下右上得分
    public static int getRight(int i, int j, int piecesTYPE, int[][] table) {
        //空白点位与当前类型棋子出现的次数
        int count = 1;
        //循环变量
        int m = i - 1;
        int n = j + 1;
        //棋盘的位置得分
        int positionScore = 0;
        //棋型得分
        int chessStyleScore = 0;

        for (m = i - 1, n = j + 1; m >= 0 && n < table_y; m--, n++) {
            if ((table[m][n] == piecesTYPE) || table[m][n] == Constant.NO_PIECES) {
                count++;
            } else {
                break;
            }
        }

        headPoint = new Point(m, n);

        for (m = i + 1, n = j - 1; m < table_x && n >= 0; m++, n--) {
            if ((table[m][n] == piecesTYPE) || table[m][n] == Constant.NO_PIECES) {
                count++;
            } else {

                break;
            }
        }
        endPoint = new Point(m, n);

        char[] points = new char[count];
        //将棋盘数据转化为棋型数据
        for (m = headPoint.x + 1, n = headPoint.y - 1; m < endPoint.x && n > endPoint.y; m++, n--) {

            if (table[m][n] == piecesTYPE) {

                //获取棋盘位置得分
                positionScore += tablePositionScore[m][n];
                points[m - (headPoint.x + 1)] = '1';
            } else if (table[m][n] == Constant.NO_PIECES) {
                points[m - (headPoint.x + 1)] = '0';
            }
        }

        points[i - (headPoint.x + 1)] = '1';

        //当前要落子的点位的得分
        positionScore += tablePositionScore[i][j];

        //存储棋型的字符串
        String chessStyle = new String(points);
        for (m = 0; m < Constant.chessStyleFives.length; m++) {
            if (chessStyle.indexOf(Constant.chessStyleFives[m].getChessStyle()) >= 0) {
                chessStyleScore = Constant.chessStyleFives[m].getChessStyleScore();
                break;
            }
        }


        Log.i("mike","左下右上得分："+positionScore + chessStyleScore);
        Log.i("mike","左下右上棋型："+chessStyle);
        Log.i("mike","---------------------------------------");


        //返回位置得分与棋型得分之和
        return positionScore + chessStyleScore;
    }


}
